/**
 * Project: TacticaPescaditos
 * File: KickOff.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Posicion;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.GoalKeeper;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.Player;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.MessageDispatcher;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: KickOff
 */
public class KickOff implements State<GoalKeeper> {
	
	/**
	 * 
	 */
	private KickOff() {
	}

	public static final State<GoalKeeper> instance = new KickOff();

	public void enter(GoalKeeper keeper) {
		keeper.getTactica().setControllingPlayer(keeper);
		keeper.getTactica().setReceiver(null);
		MessageDispatcher dispatcher = keeper.getTactica().getDispatcher();
		Vector2D ballTarget = new Vector2D();
		double power = keeper.getKickForce()*0.75;
		Player<?> receiver = keeper.getTactica().findPass(keeper, ballTarget, power,TacticaPescaditos.PASS_ANGLE);
			if ( receiver != null ) {
				keeper.kick(new Posicion(ballTarget.x,ballTarget.y),1,TacticaPescaditos.PASS_ANGLE);
				
				dispatcher.dispatch( keeper.getId(),receiver.getId(), 
						Telegram.MessageType.RECEIVE_BALL,ballTarget,0);

				keeper.getStateMachine().changeState(TendGoal.instance);
				return;
		}
			double s = (keeper.getBall().getVelocity().x<0)?-1:1;
			keeper.kick(new Posicion(s*keeper.getTactica().getHalfWidth(),keeper.getPosition().y+55),1,60 );
	}

	public void execute(GoalKeeper keeper) {
			  keeper.getStateMachine().changeState(TendGoal.instance);

	}

	public void exit(GoalKeeper keeper) {
		keeper.getSteering().pursuitOff();
		if (keeper.getTactica().getReceiver()==null)
		keeper.getTactica().setControllingPlayer(null);
	}

	public boolean onMessage(GoalKeeper keeper, Telegram telegram) {
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "KickOff";
	}

}
